﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BasicDataManagement;
using DataModel;

namespace CashierCashFlowManagement
{
    public partial class CashFlowForm : Form
    {
        OpFromDB op;
        List<String> groupNameList;
        List<String> staffNameList;
        List<StaffInfo> staffList;
        List<Reimbursementinfo> ViewReimList;
        string userJobNumber;

        public CashFlowForm(SystemUser user)
        {
            InitializeComponent();
            op = OpFromDB.GetInstance();
            groupNameList = op.LoadGroupNames();
            userJobNumber = user.StaffJobNum;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            StaffGroupcomboBox.Items.Clear();
            StaffGroupcomboBox.Items.AddRange(groupNameList.ToArray());
            if (StaffGroupcomboBox.Items.Count != 0)
            {
                StaffGroupcomboBox.Text = (String)StaffGroupcomboBox.Items[0];
            }

            StaffNamecomboBox.Items.Clear();
            staffNameList = op.LoadStaffNames(StaffGroupcomboBox.Text);
            StaffNamecomboBox.Items.AddRange(staffNameList.ToArray());
            if (StaffNamecomboBox.Items.Count != 0)
            {
                StaffNamecomboBox.Text = (string)StaffNamecomboBox.Items[0];
            }

            ReimStatecomboBox.Text = "申请报销";
            ReimTypecomboBox.Text = "全部";

        }

        private void StaffGroupcomboBox_TextChanged(object sender, EventArgs e)
        {
            StaffNamecomboBox.Items.Clear();

            staffNameList = op.LoadStaffNames(StaffGroupcomboBox.Text);
            StaffNamecomboBox.Items.AddRange(staffNameList.ToArray());
            if (StaffNamecomboBox.Items.Count != 0)
            {
                StaffNamecomboBox.Text = (string)StaffNamecomboBox.Items[0];
            }
        }

        private void ViewCashflowbutton_Click(object sender, EventArgs e)
        {

            string jobNumber = null;
            int leftIndex = 0;
            int rightIndex = 0;
            string staffName=StaffNamecomboBox.Text;
            for (int i = 0; i < staffName.Length; i++) {
                if (staffName[i] == '(') leftIndex = i + 1;
                if (staffName[i] == ')') rightIndex = i - 1;
            }
            jobNumber = staffName.Substring(leftIndex, rightIndex - leftIndex + 1);
            //计算这个人员今天实收多少现金
            List<int> billIDlist = op.loadBillIDbyStaff(jobNumber,BilldateTimePicker.Value.Date);
            if (billIDlist.Count == 0) {
                MessageBox.Show("该人员今天没收账！");
                return;
            }
            //计算这个人员今天一共结了几次帐
            CashTimestextBox.Text = billIDlist.Count.ToString();
            decimal sum = new decimal();
            foreach (int i in billIDlist) {
                decimal price = op.LoadBillPrice(i);
                System.Diagnostics.Debug.WriteLine(price);
                sum += op.LoadBillPrice(i);
            }

            dailySumCashtextBox.Text = sum.ToString();

            //查看这个人员今天收账情况
            CashflowlistView.Items.Clear();
            List<BillInfo> BillInfoList = op.loadBillbyStaff(jobNumber, BilldateTimePicker.Value.Date);
            foreach (BillInfo bi in BillInfoList)
            {
                ListViewItem litItem = new ListViewItem(new String []{bi.BillTime.ToString(),bi.EatPlace.TrimEnd(),bi.CashierIP.TrimEnd(),bi.BillPirce.ToString(),bi.Discount.ToString(),(bi.BillPirce-bi.Discount).ToString() });
                CashflowlistView.Items.Add(litItem);
            }
        }

        //现金流向

        private void ViewGiveGetbutton_Click(object sender, EventArgs e)
        {
            //给取详情
            GiveGetlistView.Items.Clear();
            List<MoneyRecordInfo> MoneyRecordList = op.LoadMoneyRecordbyDate(GiveGetdateTimePicker.Value.Date);
            foreach(MoneyRecordInfo i in MoneyRecordList){
                ListViewItem litItem = new ListViewItem(new String []{i.GiveGetTime.ToString(),i.CashierName,i.GiveorGet,i.AdminName,i.Money.ToString() });
                GiveGetlistView.Items.Add(litItem);
            }

            //人员收入详情
            StaffCashlistView.Items.Clear();
            List<CashierCashInfo> CashierCashList = op.LoadCashierCash(GiveGetdateTimePicker.Value.Date);
            if (CashierCashList.Count == 0) {
                MessageBox.Show("今日暂无收入！");
                return;
            }
            decimal sum = 0.0m;
            foreach (CashierCashInfo i in CashierCashList) {
                sum += i.ComingSum;
                ListViewItem litItem = new ListViewItem(new String[] { i.StaffName,i.ComingSum.ToString(),i.SpareSum.ToString(),i.HandinSum.ToString(),i.RemainSum.ToString() });
                StaffCashlistView.Items.Add(litItem);
            }
            //d当天一共收取了多少钱
            SumtextBox.Text = sum.ToString();

        }
        //查看余额
        private void ViewMoneybutton_Click(object sender, EventArgs e)
        {
            ViewStaffMenoylistView.Items.Clear();
            staffList = op.LoadStaffs();
            if (staffList.Count == 0)
            {
                MessageBox.Show("暂无员工信息！");
                return;
            }
            foreach (StaffInfo s in staffList) {
                ListViewItem litItem = new ListViewItem(new string[] { s.JobNumber, s.Name, s.Money.ToString() });
                ViewStaffMenoylistView.Items.Add(litItem);
            }
        }

        private void ViewStaffMenoylistView_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            if (ViewStaffMenoylistView.SelectedItems.Count != 0) {
                numbertextBox.Text = staffList[ViewStaffMenoylistView.SelectedIndices[0]].JobNumber;
                nametextBox.Text = staffList[ViewStaffMenoylistView.SelectedIndices[0]].Name;
                menoytextBox.Text = staffList[ViewStaffMenoylistView.SelectedIndices[0]].Money.ToString();

            }
        }

        private void Modifybutton_Click(object sender, EventArgs e)
        {
            op.UpdateStaffMenoy(numbertextBox.Text, Convert.ToDecimal(menoytextBox.Text));
            MessageBox.Show("修改成功！");

            //刷新列表信息
            ViewStaffMenoylistView.Items.Clear();
            staffList = op.LoadStaffs();
            if (staffList.Count == 0)
            {
                MessageBox.Show("暂无员工信息！");
                return;
            }
            foreach (StaffInfo s in staffList)
            {
                ListViewItem litItem = new ListViewItem(new string[] { s.JobNumber, s.Name, s.Money.ToString() });
                ViewStaffMenoylistView.Items.Add(litItem);
            }
        }
        //报销单处理——查看详情
        private void ViewReimbutton_Click(object sender, EventArgs e)
        {
            ViewReimlistView.Items.Clear();
            int state = -1;
            if (ReimStatecomboBox.Text.Equals("申请报销")) {
                state = 0;
            }
            else if (ReimStatecomboBox.Text.Equals("已审批"))
            {
                state = 1;
            }
            else if (ReimStatecomboBox.Text.Equals("全部"))
            {
                state = -1;
            }

            ViewReimList = op.LoadStaffReimRecord(ReimdateTimePicker.Value.Date, state, isAllcheckBox.Checked,ReimTypecomboBox.Text);
            if (ViewReimList.Count == 0) {
                MessageBox.Show("无数据记录！");
                return;
            }
            foreach (Reimbursementinfo reimInfo in ViewReimList) { 
                ViewReimlistView.Items.Add(new ListViewItem(new string[] { reimInfo.JobNumber, reimInfo.Date.ToString(), reimInfo.Project, reimInfo.Price.ToString(), reimInfo.Number.ToString(), reimInfo.Sum.ToString(), reimInfo.Reason, (reimInfo.State == 1) ? "已审批" : "申请报销" }));
            }
        }
        //批准选中报销
        private void OkReimbutton_Click(object sender, EventArgs e)
        {
            int count = ViewReimlistView.SelectedIndices.Count;
            if (count == 0)
            {
                MessageBox.Show("请选择要批准的报销单！");
                return;
            }
            else
            {
                int[] id = new int[count];
                for (int i = 0; i < count; i++)
                {
                    id[i] = ViewReimList[ViewReimlistView.SelectedIndices[i]].ID;
                }
                op.UpdateStaffReimRecord(id, userJobNumber);
                //刷新列表
                ViewReimlistView.Items.Clear();
                int state = -1;
                if (ReimStatecomboBox.Text.Equals("申请报销"))
                {
                    state = 0;
                }
                else if (ReimStatecomboBox.Text.Equals("已审批"))
                {
                    state = 1;
                }
                else if (ReimStatecomboBox.Text.Equals("全部"))
                {
                    state = -1;
                }

                ViewReimList = op.LoadStaffReimRecord(ReimdateTimePicker.Value.Date, state, isAllcheckBox.Checked,ReimTypecomboBox.Text);

                foreach (Reimbursementinfo reimInfo in ViewReimList)
                {
                    ViewReimlistView.Items.Add(new ListViewItem(new string[] { reimInfo.JobNumber, reimInfo.Date.ToString(), reimInfo.Project, reimInfo.Price.ToString(), reimInfo.Number.ToString(), reimInfo.Sum.ToString(), reimInfo.Reason, (reimInfo.State == 1) ? "已审批" : "申请报销" }));
                }
                MessageBox.Show("审批成功！");

            }
        }
    }
}
